package hkust.csit5110.android;

import hkust.csit5110.android.util.AndConstants;
import hkust.csit5110.android.util.GlobalChatListMap;
import hkust.lingx.csit5110.android.lib.client.FeedBackClient;
import hkust.lingx.csit5110.android.lib.model.AnchorMessage;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ChatView extends Activity {

  private Button chatButton;

  /**
   * chat interface
   */
  private EditText input, show, ipadd;
/*
  Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      String chatMessage = "";
      AnchorMessage anchor = (AnchorMessage) msg.obj;
      chatMessage += "From:[" + anchor.getMsgFrom() + "]";
      chatMessage += "To:[" + anchor.getMsgTo() + "]";
      chatMessage += "Content:[" + anchor.getMsgContent() + "]";
      show.append(chatMessage);
      super.handleMessage(msg);
    }
  };
*/
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.interface4);
    Log.i("===hkust===", "Enter chat activity");
    /**
     * get relative info from other activity
     */
    String from=null, to=null, content=null;
    String chatIP = getIntent().getStringExtra(AndConstants.GAE_IP);
    AnchorMessage anchor = (AnchorMessage) getIntent().getSerializableExtra("anchor");
    if (anchor != null) {
      chatIP = anchor.getMsgFrom();
      to = anchor.getMsgTo();
      content = anchor.getMsgContent();
    }
    chatButton = (Button) findViewById(R.id.button0);
    chatButton.setContentDescription(chatIP);
    input = (EditText) findViewById(R.id.editText1);
    show = (EditText) findViewById(R.id.editText2);
    ipadd = (EditText) findViewById(R.id.IP);
    /**
     *  List View should be better, this is just a simple demo
     */
    if(chatIP!=null){
      GlobalChatListMap chatHist = (GlobalChatListMap)getApplicationContext();
      Object[] chatArr=chatHist.getChatList(chatIP);
      for(Object obj:chatArr){
        show.append(obj.toString()+"\n");
      }
    }
    /*
     * Send Message from client to Server
     */
    chatButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        try {
          Log.i("===hkust===","click chat button");
          FeedBackClient client = new FeedBackClient();
          String connIp = "";
          connIp = v.getContentDescription().toString();
          if (connIp == null || connIp.trim().equalsIgnoreCase("") || connIp.trim().equalsIgnoreCase("null")) {
            connIp = ipadd.getText().toString();
          }
          try{
          client.setUpConn(connIp, AndConstants.CHAT_PORT);
          }catch(Exception e){
            Log.e("===hkust===conncet exception!!!", e.getMessage());
            show.append("The stranger is offline :(\n");
            return;
          }
          AnchorMessage anchor = new AnchorMessage();
          anchor.setMsgFrom(AndConstants.getLocalIpAddress());
          anchor.setMsgTo("Server");
          anchor.setMsgContent(input.getText().toString());
          GlobalChatListMap chatHist = (GlobalChatListMap)getApplicationContext();
          chatHist.addAnchorMessage(connIp, anchor);
          //temp test
          show.append("I said:"+anchor.getMsgContent());
          client.sendAnchorMessage(anchor);
          client.tearDownConn();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

   
}
